package one.stand.service.impl;

import one.stand.event.UpdateGoodsEvent;
import one.stand.mapper.GoodSpecMapperExt;
import one.stand.mapper.GoodsServiceCrmMapper;
import one.stand.model.GoodSpec;
import one.stand.model.Goods;
import one.stand.service.GoodsImportService;
import one.stand.service.GoodsPhotoBaseService;
import one.stand.service.GoodsTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class GoodsImportServiceImpl implements GoodsImportService {
    @Autowired
    private GoodsServiceCrmMapper goodsServiceCrmMapper;
    @Autowired
    private GoodSpecMapperExt goodSpecMapperExt;
    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private GoodsPhotoBaseService goodsPhotoBaseService;
    @Autowired
    private ApplicationContext applicationContext;

    @Override
    @Transactional
    public void saveGoods(Integer companyId, List<String> lo) {

        Date date = new Date();

        Integer goodsId = saveGoods(date, companyId, lo);

        GoodSpec goodSpecModel = new GoodSpec();
        goodSpecModel.setGoodsId(goodsId);
        goodSpecModel.setSpecName(lo.get(6));
        goodSpecModel.setSpecAttribute(lo.get(7));
        goodSpecModel.setSpecNum(Integer.valueOf(lo.get(8)));
        goodSpecModel.setPrice(new BigDecimal(lo.get(9)).intValue());
        goodSpecModel.setParentId(getSpecParentId(goodsId, goodSpecModel.getSpecName()));
        goodSpecMapperExt.insertSelective(goodSpecModel);
    }

    @Override
    @Transactional
    public void removeGoods(Integer companyId) {
        String remark = "import%";
        goodsServiceCrmMapper.deleteByRemark(companyId, remark);

        applicationContext.publishEvent(new UpdateGoodsEvent(companyId, null));
    }

    private Integer saveGoods(Date date, Integer companyId, List<String> lo) {
        String remark = "import" + lo.get(10);
        Goods goodsModel = goodsServiceCrmMapper.selectByRemark(companyId, remark);
        if (Objects.nonNull(goodsModel)) {
            return goodsModel.getGoodsId();
        }

        Integer typeId = goodsTypeService.saveImport(companyId, lo.get(1));

        goodsModel = new Goods();
        goodsModel.setGoodsName(lo.get(0));
        goodsModel.setGoodstypeId(typeId);
        goodsModel.setCompanyId(companyId);
        String detail = lo.get(2);
        detail = StringUtils.replaceAll(detail, "<img", "<img class=\"w100p\"");
        detail = StringUtils.replaceAll(detail, "style=\"\"", "style=\"display: flex;\"");
        detail = StringUtils.replaceAll(detail, "<p>", "");
        detail = StringUtils.replaceAll(detail, "</p>", "");
        goodsModel.setGoodsDetails(detail);
//        goodsModel.setIsPayment(1);//1
        goodsModel.setCreateTime(date);
        goodsModel.setIsDel(0);
        goodsModel.setSort(0);
        goodsModel.setRemark(remark);
        goodsModel.setQjVrUrl(lo.get(3));
        goodsModel.setQjVrUrl(StringUtils.isNoneBlank(goodsModel.getQjVrUrl()) ? StringUtils.replaceAll(goodsModel.getQjVrUrl(), "beyond.3dnest.cn", "cloud2.3dnest.cn") : null);
        goodsModel.setVideoUrl(lo.get(4));
        goodsServiceCrmMapper.insertSelective(goodsModel);

        String photos = lo.get(5);
        goodsPhotoBaseService.save(goodsModel.getGoodsId(), photos);

        return goodsModel.getGoodsId();
    }

    private Integer getSpecParentId(Integer goodsId, String specName) {
        GoodSpec specModel = goodSpecMapperExt.select(goodsId, specName);
        if (Objects.nonNull(specModel)) {
            return specModel.getSpecId();
        }

        specModel = new GoodSpec();
        specModel.setGoodsId(goodsId);
        specModel.setSpecName(specName);
        specModel.setSpecAttribute(null);
        specModel.setSpecNum(null);
        specModel.setPrice(null);
        specModel.setParentId(0);
        goodSpecMapperExt.insertSelective(specModel);
        return specModel.getSpecId();
    }
}
